#include "debug.h"
#include "../DDIC/AllDDIC.h"
#include "usbd_cdc_core.h"

/*RM67160 Gamma寄存器位址*/
u8 RM160_Gamma_r[22]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x0A,0x0B,0x10,0x11,0x14,0x15,0x18,0x19,0x1C,0x1D,0x20,0x21,0x32,0x33};
u8 RM160_Gamma_g[22]={0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x40,0x41,0x46,0x47,0x4A,0x4B,0x4E,0x4F,0x52,0x53,0x56,0x58,0x67,0x68};
u8 RM160_Gamma_b[22]={0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x73,0x74,0x79,0x7A,0x7D,0x7E,0x81,0x82,0x85,0x86,0x89,0x8A,0x99,0x9A};

/*RM67162 Gamma寄存器位址*/
u8 RM162_Gamma_r[50]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0c,0x0D,0x0e,0x0f,0x10,0x11,0x12,0x13,0x14,0x15,
                        0x16,0x17,0x18,0x19,0x1a,0x1b,0x1C,0x1D,0x1e,0x1f,0x20,0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x2a,0x2b,0x2d,0x2f,0x30,0x31,0x32,0x33};
u8 RM162_Gamma_g[50]={0x34,0x35,0x36,0x37,0x38,0x39,0x3A,0x3B,0x3d,0x3f,0x40,0x41,0x42,0x43,0x44,0x45,0x46,0x47,0x48,0x49,0x4A,0x4B,
                        0x4c,0x4d,0x4E,0x4F,0x50,0x51,0x52,0x53,0x54,0x55,0x56,0x58,0x59,0x5a,0x5b,0x5c,0x5d,0x5e,0x5f,0x60,0x61,0x62,0x63,0x64,0x65,0x66,0x67,0x68};
u8 RM162_Gamma_b[50]={0x69,0x6A,0x6B,0x6C,0x6D,0x6E,0x6F,0x70,0x71,0x72,0x73,0x74,0x75,0x76,0x77,0x78,0x79,0x7A,0x7b,0x7c,0x7D,0x7E,
                        0x7f,0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,0x88,0x89,0x8A,0x8b,0x8c,0x8d,0x8e,0x8f,0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,0x98,0x99,0x9A};

/********************************************************************************************
********************************************************************************************
    RM67160、RM67162  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67160、RM67162  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67160、RM67162  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67160、RM67162  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
    RM67160、RM67162  写寄存器 读寄存器  写Gamma 读Gamma OTP烧录
********************************************************************************************
********************************************************************************************/
void RM6716X_Write_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {
        SPI_Write_code(SigMode,channel,buffer[3],buffer[4]);  
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        SSD2828_W_Array(SigMode,channel,buffer,2);
        delay_ms(50);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写寄存器状态  ：ok  
}

void RM6716X_Read_Register(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if(SigMode == SPI3_Mode)          //3线8bit SPI 模式
    {

    }
    else if(SigMode == SPI4_Mode)          //4线8bit SPI 模式
    {
    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002); 
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);                                    
        for(i=0;i<3;i++)
        {
            SSD2828_W_Cmd(SigMode,channel,0xBF);
            SSD2828_W_Data(SigMode,channel,buffer[3]);
            if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   
                SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);  
            delay_ms(25);
            buffer[4]=SSD2828_R_Reg(SigMode,channel,0xFF);
        }                              
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(5);
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 寄存器数据  ：ok  	
    }
}

void RM6716X_Write_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 buffer1[3];
    u8 gamma_write_num;
    if(buffer[0]==RM67160)
    {
        gamma_write_num=22;
    }
    else if(buffer[0]==RM67162)
    {
        gamma_write_num=50;
    }
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {                                   
        if(buffer[1] == 0x08)
            SPI_Write_code(SigMode,channel,0xFE,0x02);
        else if(buffer[1] == 0x18)
            SPI_Write_code(SigMode,channel,0xFE,0x03);                                 
        for( i = 0;i<gamma_write_num;i++)
        {	                                                                   
            SPI_Write_code(SigMode,channel,((buffer[3]==0x00) ? ((buffer[0]==RM67160) ? RM160_Gamma_r[i] : RM162_Gamma_r[i]) : \
                                           ((buffer[3]==0x34) ? ((buffer[0]==RM67160) ? RM160_Gamma_g[i] : RM162_Gamma_g[i]) : \
                                                                ((buffer[0]==RM67160) ? RM160_Gamma_b[i] : RM162_Gamma_b[i]))),buffer[i+4]);     
            delay_ms(1);
        }                                    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = (buffer[1] == 0x08) ? 0x02 : 0x03;
        SSD2828_W_Array(SigMode,channel,buffer1,0);   
        for( i = 0;i<gamma_write_num;i++)
        {	                                                                   
            SSD2828_W_Reg(SigMode,channel,0xBF,  ((buffer[3]==0x00) ? ((buffer[i+4]<<8)+((buffer[0]==RM67160) ? RM160_Gamma_r[i] : RM162_Gamma_r[i])) : \
                                                        ((buffer[3]==0x34) ? ((buffer[i+4]<<8)+((buffer[0]==RM67160) ? RM160_Gamma_g[i] : RM162_Gamma_g[i])) : \
                                                                                    ((buffer[i+4]<<8)+((buffer[0]==RM67160) ? RM160_Gamma_b[i] : RM162_Gamma_b[i])))));      //RM67160
            delay_ms(1);
        }	
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);                              
    }
    buffer[4] = Uart_Error_None;
    buffer[1] =  0x08;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 写Gamma状态  ：ok 
}

void RM6716X_auto_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {                                                                  
        for( i = 0;i<buffer[2];i++)
        {	                                                                   
            SPI_Write_code(SigMode,channel,buffer[i+3],buffer[i+4]);     
            i++;
        }                                    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);  
        for( i = 0;i<buffer[2];i++)
        {	                                                                   
            SSD2828_W_Reg(SigMode,channel,0xBF,(buffer[i+4]<<8)+buffer[i+3]);      //RM67160
            i++;
        }	
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);                              
    }
}

void RM6716X_Read_Gamma(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 gamma_read_num;
    if(buffer[0]==RM67160)
    {
        gamma_read_num=22;
    }
    else if(buffer[0]==RM67162)
    {
        gamma_read_num=50;
    }
    if(SigMode == SPI3_Mode)          //3线8bit SPI 模式
    {
 
    }
    else if(SigMode == SPI4_Mode)          //4线8bit SPI 模式
    {
    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0002);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);   

        if(buffer[0]==RM67160)
        {
            if(buffer[1] == 0x09)
                SSD2828_W_Reg(SigMode,channel,0xBF,0x02FE);
            else if(buffer[1] == 0x19)
                SSD2828_W_Reg(SigMode,channel,0xBF,0x03FE);  
        }
        else if(buffer[0]==RM67162)
        {
            if(buffer[1] == 0x09)
                SSD2828_W_Reg(SigMode,channel,0xBF,0xA2FE);
            else if(buffer[1] == 0x19)
                SSD2828_W_Reg(SigMode,channel,0xBF,0xA3FE);  
        }
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
//        SSD2828_W_Reg(SigMode,channel,0xC1,0x0001); 
//        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001); 
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
                            
        for( i = 0;i<gamma_read_num;i++)							
        {   
                SSD2828_W_Cmd(SigMode,channel,0xBF);
                SSD2828_W_Data(SigMode,channel,(buffer[3]==0x00) ? ((buffer[0]==RM67160) ? RM160_Gamma_r[i] : RM162_Gamma_r[i]) : \
                                               (buffer[3]==0x34) ? ((buffer[0]==RM67160) ? RM160_Gamma_g[i] : RM162_Gamma_g[i]) : \
                                                                   ((buffer[0]==RM67160) ? RM160_Gamma_b[i] : RM162_Gamma_b[i]));
                delay_ms(25);
                if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
                    SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA); 
                buffer[i+4]=SSD2828_R_Reg(SigMode,channel,0xFF);
        }                   
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);	                              
    }
    buffer[1] = (buffer[0]==RM67160) ? 0x09 : 0x0C;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的 Gamma数据  ：ok 
}
void RM6716X_Gamma_OTP_Start(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{
    u8 tmp;
	  u8 buffer1[3];
    if((SigMode == SPI3_Mode)||(SigMode == SPI4_Mode))          //3线 4线8bit SPI 模式
    {
        delay_ms(120); 
        SPI_Write_code(SigMode,channel,0xfe,0x00);
        SPI_Write_u8_Cmd(SigMode,channel,0x28); 
        delay_ms(100);  //otp
        SPI_Write_code(SigMode,channel,0xfe,0x01);
        SPI_Write_code(SigMode,channel,0xf0,0x10);
        SPI_Write_code(SigMode,channel,0xf2,0x03);
        SPI_Write_code(SigMode,channel,0xf3,0xA5); 
        SPI_Write_code(SigMode,channel,0xf4,0x5a);
        SPI_Write_code(SigMode,channel,0xf5,0x3c);
        delay_ms(500);         // wait for >400 ms 
        
        
        buffer[4] = Uart_Error_None;
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP状态  ：ok
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        delay_ms(120); 
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x00;
        SSD2828_W_Array(SigMode,channel,buffer1,0);                                                                      

        buffer1[0] = 0x01;
        buffer1[1] = 0x28;
        SSD2828_W_Array(SigMode,channel,buffer1,0);   
        delay_ms(200);  //otp
        buffer1[0] = 0x02;
        buffer1[1] = 0xFE;
        buffer1[2] = 0x01;
        SSD2828_W_Array(SigMode,channel,buffer1,0);  
        buffer1[0] = 0x02;
        buffer1[1] = 0xF0;       
				buffer1[2] = buffer[3];				
        SSD2828_W_Array(SigMode,channel,buffer1,0);                                        
				
        delay_ms(200); 
        buffer1[1] = 0xF2; buffer1[2] = 0x03;
        SSD2828_W_Array(SigMode,channel,buffer1,0); 
        
        buffer1[1] = 0xF3; buffer1[2] = 0xA5;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        
        buffer1[1] = 0xF4; buffer1[2] = 0x5A;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        
        buffer1[1] = 0xF5; buffer1[2] = 0x3C;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        delay_ms(800);         // wait for >400 ms   
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);
        SSD2828_W_Reg(SigMode,channel,0xC1,0x0002);
        SSD2828_W_Reg(SigMode,channel,0xC0,0x0001);
        buffer1[0] = 0x01;
        buffer1[1] = 0xEF;
        SSD2828_W_Array(SigMode,channel,buffer1,0);
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI   烧录OTP
            SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);
        delay_ms(100);															   								
        tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        if (tmp&0x02)       //出错
        {
            buffer[4] = Uart_Error_Oth;
        }
        else                //正确
            buffer[4] = Uart_Error_None; 
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP状态  ：ok                                       
    }
}

void RM6716X_Read_OTP_Time(USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8* buffer,u16 LP_B7_Data,u16 HS_B7_Data)
{     
    u8 tmp;
    if(SigMode == SPI3_Mode)          //3线8bit SPI 模式
    {
 
    }
    else if(SigMode == SPI4_Mode)          //4线8bit SPI 模式
    {
    
    }
    else if((SigMode == Mipi_Mode)||(SigMode == CMD_Mode))
    {
        if(SigMode != CMD_Mode)
            SSD2828_W_Reg(SigMode,channel,0xB7,(HS_B7_Data|0x0010)&0XFFFD);
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0002);
        SSD2828_W_Reg(SigMode,channel,0xBD,0x0000);
        
        if(buffer[0]==RM67160)
        {
            SSD2828_W_Reg(SigMode,channel,0xBF,0x01FE); 
        }
        else if(buffer[0]==RM67162)
        {
            SSD2828_W_Reg(SigMode,channel,0xBF,0xA1FE);   
        } 
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI
            SSD2828_W_Reg(SigMode,channel,0xB7,((HS_B7_Data|0x0010)&0XFFFD)|0x0080);      
        else
            SSD2828_W_Reg(SigMode,channel,0xB7,LP_B7_Data|0x0080);        
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xBC,0x0001);
        SSD2828_W_Cmd(SigMode,channel,0xBF);
        SSD2828_W_Data(SigMode,channel,0xEE);
        if(SigMode == Mipi_Mode)  //RGB+SPI= MIPI  
            SSD2828_W_Reg(SigMode,channel,0xD4,0x00FA);							
        delay_ms(50);                                 
        tmp=SSD2828_R_Reg(SigMode,channel,0xFF); 
        
        if (tmp&0x10)
            buffer[4] = 1;
        else if (tmp&0x08)
            buffer[4] = 2;
        else
            buffer[4] = 0;
        delay_ms(5);
        SSD2828_W_Reg(SigMode,channel,0xB7,HS_B7_Data);
        buffer[3] = Uart_Error_None;
        STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 读取的OTP数据  ：ok                                  
    }
    buffer[4] = Uart_Error_None;
    STM2PC_RM671xx(pdev,CDC_IN_EP,buffer,buffer[2]+3);           //返回 OTP状态  ：ok

}


void ProcessForIc1E( USB_OTG_CORE_HANDLE *pdev,uint8_t  ep_addr,u8 SigMode ,u8 channel,u8 buffer[],u16 LP_B7_Data,u16 HS_B7_Data)
{	
	//void *pdev;
	switch(buffer[1])
	{
		case 0x01:                                      //D′??′??÷
			RM6716X_Write_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x02:                                      //?á??′??÷
			RM6716X_Read_Register(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x08:                                      //D′Gamma
			RM6716X_Write_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		case 0x09:                                      //?áGamma
			RM6716X_Read_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
		case 0x0A:			//OTP Start
			RM6716X_Gamma_OTP_Start(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
		case 0x0B:                                      //Read OTP Time
			RM6716X_Read_OTP_Time(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data); break;
		case 0xDD:                                      // auto Gamma
			RM6716X_auto_Gamma(pdev,CDC_IN_EP,OLED.SigMode ,channel,USB_Rx_Buffer,LP_B7_Data,HS_B7_Data);break;
		default:break;
	}			
}


